package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import org.checkerframework.checker.units.qual.A;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.ActivityPubAlbumBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class PubAlbumActivity extends BaseAc<ActivityPubAlbumBinding> {

    private AlbumAdapter albumAdapter;
    private Dialog myAddAlbumDialog;
    private EditText dialogAddAlbum;

    @Override
    protected int onCreate() {
        return R.layout.activity_pub_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPubAlbumBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivPublicAlbumAdd.setOnClickListener(this);
        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 2));
        albumAdapter = new AlbumAdapter();
        albumAdapter.setOnItemClickListener(this);
        //添加相册弹框
        addAlbumDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPublicAlbumAdd://添加
                myAddAlbumDialog.show();
                break;
            //添加相册弹框
            case R.id.tvDialogAddAlbumCancel://取消
                myAddAlbumDialog.dismiss();
                break;
            case R.id.tvDialogAddAlbumRight://确定
                addAlbum();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String selId = albumAdapter.getItem(position).getId();
        if (TextUtils.isEmpty(selId)) {
            //添加公开相册
            dialogAddAlbum.setText("");
            myAddAlbumDialog.show();
        } else {
            //查看公开相册
            PubAlbumDetailActivity.albumId = selId;
            startActivity(PubAlbumDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        List<MyAlbumBean> listSp = AppSpUtils.getPublicAlbumList();
        if (listSp != null && listSp.size() > 1) {
            mDataBinding.rvAlbumList.setAdapter(albumAdapter);
            albumAdapter.setList(listSp);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llPublicAlbumNoData.setVisibility(View.GONE);
        } else {
            if (listSp == null || listSp.size() == 0) {
                //准备新数据
                List<MyAlbumBean> list = new ArrayList<>();
                list.add(new MyAlbumBean("", "", new ArrayList<>()));
                AppSpUtils.savePublicAlbumList(list);
            }
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.llPublicAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    //添加公开相册
    private void addAlbum() {
        String albumName = dialogAddAlbum.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myAddAlbumDialog.dismiss();
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        listSave.add(new MyAlbumBean(TimeUtils.getNowString(), albumName, new ArrayList<>()));
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getPublicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.savePublicAlbumList(listSp);
        } else {
            AppSpUtils.savePublicAlbumList(listSave);
        }
        SelPicActivity.isMore = true;
        SelPicActivity.type = 9;
        Intent intent = new Intent(this, SelPicActivity.class);
        startActivityForResult(intent, Content.REQUEST_ADD);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Content.REQUEST_ADD) {
            List<String> listPath = (List<String>) data.getSerializableExtra(Content.LIST_PATH);
            if (listPath != null && listPath.size() > 0) {
                List<MyAlbumBean> listSp = AppSpUtils.getPublicAlbumList();
                if (listSp != null && listSp.size() > 0) {
                    MyAlbumBean bean = listSp.get(listSp.size() - 1);
                    if (bean.getListData() != null && bean.getListData().size() > 0) {
                        bean.getListData().addAll(listPath);
                    } else {
                        bean.setListData(listPath);
                    }
                    AppSpUtils.savePublicAlbumList(listSp);
                }
                getData();
            }
        }
    }

    //添加相册弹框
    private void addAlbumDialog() {
        myAddAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_album, null);
        myAddAlbumDialog.setContentView(inflate);
        myAddAlbumDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myAddAlbumDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.95);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogClose = inflate.findViewById(R.id.tvDialogAddAlbumCancel);
        TextView dialogCom = inflate.findViewById(R.id.tvDialogAddAlbumRight);
        dialogClose.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogAddAlbum = inflate.findViewById(R.id.etDialogAddAlbum);
    }

}